<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php include_once( "header.php" ); //Header ?>
<?php include_once( "footer.php" ); //Footer ?>
<?php
//include page header
header_template("Product Frontage", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"], "icon-tag");

//db OBJECT invoked!
$db = new DATABASE();
?>
      <div class="row">
        <div class="span6">
<?php
if (isset($_GET["prod_id"])) {
  //construct SELECT query
  $conds = array();
  $conds[] = array(0); //$param_col;
  $conds[] = array(0); //$param_op;
  $conds[] = array($_GET["prod_id"]); //$param_key;
  $query = $db->db_select("product", NULL, $conds);
  //execute SELECT query
  $result = $db->database->query($query);
  //result SELECT query
  $resProd = $result->fetchAll(SQLITE_ASSOC);

  if (!empty($resProd)) {

    $img_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic";
    $img_href_pri = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic&primary=yes";
    $cat_href =  $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=cat";

    $img_del_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic-del";//.&pic_id=pid
    $img_del_pri_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic-del&primary=yes";//.&pic_id=pid
    $img_add_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic-add";//.&pic_id=pid
    $img_add_pri_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic-add&primary=yes";//.&pic_id=pid
    $img_pri_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic-pri";//.&pic_id=pid
    
    $cat_add_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=cat-add";//.&cat_id=cid
    $cat_del_href = $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=cat-del";//.&cat_id=cid

    //ACTION!
    if (isset($_GET["action"])) {
      //construct SELECT query
      $cols = array();
      $cols = array(1);
      $conds = array();
      $conds[] = array(2); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["prod_id"]); //$param_key;
      $query = $db->db_select("picture2product", $cols, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resPic2Prod = $result->fetchAll(SQLITE_ASSOC);
      //construct SELECT query
      $cols = array();
      $cols = array(1);
      $conds = array();
      $conds[] = array(2); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["prod_id"]); //$param_key;
      $query = $db->db_select("category2product", $cols, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resCat2Prod = $result->fetchAll(SQLITE_ASSOC);

      if (  ($_GET["action"] == "pic-del")
         || ($_GET["action"] == "pic-add")
         || ($_GET["action"] == "pic-pri")
         ) {
        //construct SELECT query
        $cols = array();
        $cols = array(0);
        $conds = array();
        $conds[] = array(0); //$param_col;
        $conds[] = array(0); //$param_op;
        $conds[] = array($_GET["pic_id"]); //$param_key;
        $query = $db->db_select("picture", $cols, $conds);
        //execute SELECT query
        $result = $db->database->query($query);
        //result SELECT query
        $resPic = $result->fetchAll(SQLITE_ASSOC);
      }
      
      if (  ($_GET["action"] == "cat-del")
         || ($_GET["action"] == "cat-add")
         ) {
        //construct SELECT query
        $cols = array();
        $cols = array(0);
        $conds = array();
        $conds[] = array(0); //$param_col;
        $conds[] = array(0); //$param_op;
        $conds[] = array($_GET["cat_id"]); //$param_key;
        $query = $db->db_select("category", $cols, $conds);
        //execute SELECT query
        $result = $db->database->query($query);
        //result SELECT query
        $resCat = $result->fetchAll(SQLITE_ASSOC);
      }
      
      if ($_GET["action"] == "pic-del") {
        //Validity check OK
        if (!empty($resPic)) {
          //Existence check OK
          if (in_array_2d($_GET["pic_id"], $resPic2Prod)) {
            //construct DELETE query
            $conds = array();
            $conds[] = array(1, 2); //$param_col;
            $conds[] = array(0, 0); //$param_op;
            $conds[] = array($_GET["pic_id"], $_GET["prod_id"]); //$param_key;
            $query = $db->db_delete("picture2product", $conds);
            //execute DELETE query
            $result = $db->database->query($query);
            //Remove product photo NG
            if ($result == FALSE) {
              printAlert("error", "Oh snap!", "Cannot remove photo.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            } else {//Remove product photo OK
              //DELETE SUCCEED!!!
              printAlert("success", "Well done!", "Photo successfully removed.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            }
          } else {//Existence check NG
            $error_msg = "Photo not exist.";
            printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          }
        } else {//Validity check NG
          $error_msg = "Photo not valid.";
          printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
        }
        //Primary product photo?
        if ($_GET["primary"] == "yes") {
          //construct UPDATE query
          $params = array();
          $params[] = array(5); //$param_col;
          $params[] = array("0"); //$param_key;
          $conds = array();
          $conds[] = array(0); //$param_col;
          $conds[] = array(0); //$param_op;
          $conds[] = array($_GET["prod_id"]); //$param_key;
          $query = $db->db_update("product", $params, $conds);
          //execute UPDATE query
          $result = $db->database->query($query);
          //Edit product NG
          if ($result == FALSE) {
            printAlert("error", "Oh snap!", "Cannot remove primary photo.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          } else {//Edit product OK
            //UPDATE SUCCEED!!!
            //printAlert("success", "Well done!", "Primary photo successfully removed.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          }
        }
      }

      if ($_GET["action"] == "pic-add") {
        //Validity check OK
        if (!empty($resPic)) {
          //Max photo per product OK
          if (count($resPic2Prod) < 6) {
            //Existence check OK
            if (!in_array_2d($_GET["pic_id"], $resPic2Prod)) {
              //construct INSERT query
              $params = array();
              $params[] = array(1, 2); //$param_col;
              $params[] = array($_GET["pic_id"], $_GET["prod_id"]); //$param_key;
              $query = $db->db_insert("picture2product", $params);
              //execute INSERT query
              $result = $db->database->query($query);
              //Add product photo NG
              if ($result == FALSE) {
                printAlert("error", "Oh snap!", "Cannot add photo.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
              } else { //Add product photo OK
                //INSERT SUCCEED!!!
                printAlert("success", "Well done!", "Photo successfully added.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
              }
            } else { //Existence check NG
              $error_msg = "Photo already added.";
              printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            }
          } else { //Max photo per product NG
              $error_msg = "Product already has 6 photos.";
              printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          }
        } else {//Validity check NG
          $error_msg = "Photo not valid.";
          printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
        }
        //Primary product photo?
        if ($_GET["primary"] == "yes") {
          //construct UPDATE query
          $params = array();
          $params[] = array(5); //$param_col;
          $params[] = array($_GET['pic_id']); //$param_key;
          $conds = array();
          $conds[] = array(0); //$param_col;
          $conds[] = array(0); //$param_op;
          $conds[] = array($_GET["prod_id"]); //$param_key;
          $query = $db->db_update("product", $params, $conds);
          //execute UPDATE query
          $result = $db->database->query($query);
          //Edit product NG
          if ($result == FALSE) {
            printAlert("error", "Oh snap!", "Cannot add primary photo.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          } else {//Edit product OK
            //UPDATE SUCCEED!!!
            //printAlert("success", "Well done!", "Primary photo successfully added.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          }
        }
      }

      if ($_GET["action"] == "pic-pri") {
        //Validity check OK
        if (!empty($resPic)) {
          //Existence check OK
          if (in_array_2d($_GET['pic_id'], $resPic2Prod)) {
            //Not primary product photo OK
            if ($resProd[0]['picture_id'] != $_GET['pic_id']) {
              //construct UPDATE query
              $params = array();
              $params[] = array(5); //$param_col;
              $params[] = array($_GET["pic_id"]); //$param_key;
              $conds = array();
              $conds[] = array(0); //$param_col;
              $conds[] = array(0); //$param_op;
              $conds[] = array($_GET["prod_id"]); //$param_key;
              $query = $db->db_update("product", $params, $conds);
              //execute UPDATE query
              $result = $db->database->query($query);
              //Edit product NG
              if ($result == FALSE) {
                printAlert("error", "Oh snap!", "Cannot set primary photo.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
              } else {//Edit product OK
                //UPDATE SUCCEED!!!
                printAlert("success", "Well done!", "Photo successfully set as primary.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
              }
            } else {//Not primary product photo NG
              $error_msg = "Photo is primary.";
              printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            }
          } else {//Existence check NG
            $error_msg = "Photo not exist.";
            printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          }
        } else {//Validity check NG
          $error_msg = "Photo not valid.";
          printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
        }
      }

      if ($_GET["action"] == "cat-del") {
        //Validity check OK
        if (!empty($resCat)) {
          //Existence check OK
          if (in_array_2d($_GET["cat_id"], $resCat2Prod)) {
            //construct DELETE query
            $conds = array();
            $conds[] = array(1, 2); //$param_col;
            $conds[] = array(0, 0); //$param_op;
            $conds[] = array($_GET['cat_id'], $_GET['prod_id']); //$param_key;
            $query = $db->db_delete("category2product", $conds);
            //execute DELETE query
            $result = $db->database->query($query);
            //Remove product category NG
            if ($result == FALSE) {
              printAlert("error", "Oh snap!", "Cannot remove category.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            } else {//Remove product category OK
              //DELETE SUCCEED!!!
              printAlert("success", "Well done!", "Category successfully removed.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            }
          } else {//Existence check NG
            $error_msg = "Category not exist.";
            printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          }
        } else {//Validity check NG
          $error_msg = "Category not valid.";
          printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
        }
      }
      
      if ($_GET["action"] == "cat-add") {
        //Validity check OK
        if (!empty($resCat)) {
          //Existence check OK
          if (!in_array_2d($_GET["cat_id"], $resCat2Prod)) {
            //construct INSERT query
            $params = array();
            $params[] = array(1, 2); //$param_col;
            $params[] = array($_GET['cat_id'], $_GET['prod_id']); //$param_key;
            $query = $db->db_insert("category2product", $params);
            //execute INSERT query
            $result = $db->database->query($query);
            //Add product category NG
            if ($result == FALSE) {
              printAlert("error", "Oh snap!", "Cannot add category.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            } else { //Add product category OK
              //INSERT SUCCEED!!!
              printAlert("success", "Well done!", "Category successfully added.", $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
            }
          } else {//Existence check NG
            $error_msg = "Category already added.";
            printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
          }
        } else {//Validity check NG
          $error_msg = "Category not valid.";
          printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
        }
      }
    }
?>
          <div class="page-header">
            <h3><?php echo $resProd[0]['product_name']; ?></h3>
          </div>
<?php
    //INTERFACE!
/*---SELECT PHOTO START---*/
    if ($_GET["action"] == "pic") {
      //Primary product photo?
      if ($_GET["primary"] == "yes") {
        $img_href = "&prod_id=".$_GET["prod_id"]."&action=pic&primary=yes";
      } else {
        $img_href = "&prod_id=".$_GET["prod_id"]."&action=pic";
      }

      //Pagination
      //set number of pic / page
      $picPerPage = 6;
      //initialize current page
      $curPage = 0;
      //set first page
      $firstPage = 1;
      //initialize last page
      $cols = "count";
      $conds = NULL;
      $query = $db->db_select("picture", $cols, $conds);
      //$query = "SELECT count(*) FROM picture";
      $rowCount = $db->database->query($query);
      $rowCount = $rowCount->fetchAll(SQLITE_NUM);
      $rowCount = intval($rowCount[0][0]);
      $remainder = intval($rowCount % $picPerPage);
      $lastPage = intval($rowCount / $picPerPage);
      //set current page
      if ((isset($_GET["page"])) && (is_numeric($_GET["page"]))) {
        $curPage = intval($_GET["page"]);
      } else {
        $curPage = $firstPage;
      }
      //set last page
      if ($remainder > 0) {
        $lastPage++;
      }
      if ($lastPage > 1) { //pagination start
?>
          <ul class="pager">
<?php
      if ($curPage != $firstPage) {
        $pager_prev_state = " active";
        $pager_prev_href  = " href=\"".$_SERVER['PHP_SELF']."?page=".($curPage-1).$img_href."\"";
      } else {
        $pager_prev_state = " disabled";
        $pager_prev_href  = "";
      }

      if ($curPage != $lastPage) {
        $pager_next_state = " active";
        $pager_next_href  = " href=\"".$_SERVER['PHP_SELF']."?page=".($curPage+1).$img_href."\"";
      } else {
        $pager_next_state = " disabled";
        $pager_next_href  = "";
      }
?>
            <li class="previous <?php echo $pager_prev_state; ?>">
              <a<?php echo $pager_prev_href; ?>>&larr; Prev</a>
            </li>
            <li class="next <?php echo $pager_next_state; ?>">
              <a<?php echo $pager_next_href; ?>>Next &rarr;</a>
            </li>
          </ul>
          <center>
            <p><span class="label label-info">Page <?php echo $curPage; ?>/<?php echo $lastPage; ?></span></p>
          </center>
<?php } //pagination end ?>
<?php
      //construct SELECT query
      $query = $db->db_select("picture", NULL, NULL);
      //execute SELECT query
      $result = $db->database->query($query." ORDER BY picture_id DESC LIMIT ".(($curPage-1)*$picPerPage).", ".$picPerPage);
      //result SELECT query
      $resPic = $result->fetchAll(SQLITE_ASSOC);
      //List File(s)
      //File(s) existence OK
      if (!empty($resPic)) {
?>
          <ul class="thumbnails">
<?php
        //loop trough all file(s)
        foreach ($resPic as $value) {
          //construct SELECT query
          $conds = array();
          $conds[] = array(1, 2); //$param_col;
          $conds[] = array(0, 0); //$param_op;
          $conds[] = array($value['picture_id'], $_GET["prod_id"]); //$param_key;
          $query = $db->db_select("picture2product", NULL, $conds);
          //execute SELECT query
          $result = $db->database->query($query);
          //result SELECT query
          $resPic2Prod = $result->fetchAll(SQLITE_ASSOC);
          $img_src = $value['picture_name'];
          $img_src_resize = "timthumb.php?src=".$img_src."&w=160&h=120&a=t";
          if (empty($resPic2Prod)) {
            //Primary product photo?
            if ($_GET["primary"] == "yes") {
              $img_add_href = "href=\"".$_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic-add&primary=yes&pic_id=".$value['picture_id']."\" ";
            } else {
              $img_add_href = "href=\"".$_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]."&action=pic-add&pic_id=".$value['picture_id']."\" ";
            }
            $img_add_lnk_state = "";
          } else {
            $img_add_href = "";
            $img_add_lnk_state = " disabled";
          }
?>
            <li class="span2">
              <div class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>" alt="<?php echo $img_src; ?>">
                <div class="caption">
                  <a <?php echo $img_add_href; ?>class="btn btn-mini btn-success<?php echo $img_add_lnk_state; ?>">Add</a>
                  <a href="<?php echo $img_src; ?>" class="btn btn-mini btn-default" target="_blank">Full Size</a>
                </div>
              </div>
            </li>
<?php
        }
?>
          </ul>
          <hr />
<?php
      }
/*---SELECT PHOTO END---*/
    } elseif ($_GET["action"] == "cat") {
/*---SELECT CATEGORY START---*/
      //construct SELECT query
      $query = $db->db_select("category", NULL, NULL);
      //execute SELECT query
      $result = $db->database->query($query." ORDER BY category_id DESC");
      //result SELECT query
      $cat = $result->fetchAll(SQLITE_ASSOC);
      //List Category(ies)
      //Category(ies) existence OK
      if (!empty($cat)) {
        //construct SELECT query
        $cols = array();
        $cols = array(1);
        $conds = array();
        $conds[] = array(2); //$param_col;
        $conds[] = array(0); //$param_op;
        $conds[] = array($_GET["prod_id"]); //$param_key;
        $query = $db->db_select("category2product", $cols, $conds);
        //execute SELECT query
        $result = $db->database->query($query);
        //result SELECT query
        $resCat2Prod = $result->fetchAll(SQLITE_ASSOC);
?>
          <p><span class="label label-inverse">Add Category</span></p>
          <p>
<?php
        foreach ($cat as $value) {
          if (!in_array_2d($value['category_id'], $resCat2Prod)) {
?>
            <a href="<?php echo $cat_add_href."&cat_id=".$value['category_id']; ?>" class="btn btn-mini btn-success"><?php echo $value['category_name']; ?></a>
<?php
          }
        }
?>
          </p>
          <p><span class="label label-inverse">Remove Category</span></p>
          <p>
<?php
        foreach ($cat as $value) {
          if (in_array_2d($value['category_id'], $resCat2Prod)) {
?>
            <a href="<?php echo $cat_del_href."&cat_id=".$value['category_id']; ?>" class="btn btn-mini btn-danger"><?php echo $value['category_name']; ?></a>
<?php
          }
        }
?>
          </p>
<?php
      }
?>
          <hr />
<?php
/*---SELECT CATEGORY END---*/
    } else {
/*---PRODUCT FRONTAGE START---*/
      //get latest info from database
      //construct SELECT query
      $conds = array();
      $conds[] = array(0); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["prod_id"]); //$param_key;
      $query = $db->db_select("product", NULL, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resProd = $result->fetchAll(SQLITE_ASSOC);
      //construct SELECT query
      $cols = array();
      $cols = array(1);
      $conds = array();
      $conds[] = array(2); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["prod_id"]); //$param_key;
      $query = $db->db_select("picture2product", $cols, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resPic2Prod = $result->fetchAll(SQLITE_ASSOC);
      //construct SELECT query
      $cols = array();
      $cols = array(1);
      $conds = array();
      $conds[] = array(2); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["prod_id"]); //$param_key;
      $query = $db->db_select("category2product", $cols, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resCat2Prod = $result->fetchAll(SQLITE_ASSOC);
?>
          <ul class="thumbnails">
<?php
      if ($resProd[0]['picture_id'] != 0) {
        //construct SELECT query
        $cols = array();
        $cols = array(1);
        $conds = array();
        $conds[] = array(0); //$param_col;
        $conds[] = array(0); //$param_op;
        $conds[] = array($resProd[0]['picture_id']); //$param_key;
        $query = $db->db_select("picture", $cols, $conds);
        //execute SELECT query
        $result = $db->database->query($query);
        //result SELECT query
        $resPic = $result->fetchAll(SQLITE_ASSOC);
        $img_src = $resPic[0]['picture_name'];
        $img_src_resize = "timthumb.php?src=".$img_src."&w=360&h=310&a=t";
?>
            <li class="span4">
              <div class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>">
                <div class="caption">
                  <a class="btn btn-danger btn-mini" href="<?php echo $img_del_pri_href."&pic_id=".$resProd[0]['picture_id']; ?>">Remove</a>
                  <a class="btn btn-success btn-mini disabled">Primary</a>
                </div>
              </div>
            </li>
<?php
      } else {
        $img_src = "http://placehold.it/";
        $img_src_resize = $img_src."360x310";
?>
            <li class="span4">
              <div class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>">
                <div class="caption">
                  <a class="btn btn-info btn-mini" href="<?php echo $img_href_pri; ?>">Add</a>
                  <a class="btn btn-success btn-mini disabled">Primary</a>
                </div>
              </div>
            </li>
<?php
      }
      $picCount = 0;
      foreach ($resPic2Prod as $key => $value) {
        if ($resPic2Prod[$key]['picture_id'] != $resProd[0]['picture_id']) {
          $picCount++;
          //construct SELECT query
          $cols = array();
          $cols = array(1);
          $conds = array();
          $conds[] = array(0); //$param_col;
          $conds[] = array(0); //$param_op;
          $conds[] = array($resPic2Prod[$key]['picture_id']); //$param_key;
          $query = $db->db_select("picture", $cols, $conds);
          //execute SELECT query
          $result = $db->database->query($query);
          //result SELECT query
          $resPic = $result->fetchAll(SQLITE_ASSOC);

          $img_src = $resPic[0]['picture_name'];
          $img_src_resize = "timthumb.php?src=".$img_src."&w=160&h=120&a=t";
?>
            <li class="span2">
              <div class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>">
                <div class="caption">
                  <a class="btn btn-danger btn-mini" href="<?php echo $img_del_href."&pic_id=".$resPic2Prod[$key]['picture_id']; ?>">Remove</a>
                  <a class="btn btn-success btn-mini" href="<?php echo $img_pri_href."&pic_id=".$resPic2Prod[$key]['picture_id']; ?>">Primary</a>
                </div>
              </div>
            </li>
<?php
        } else {
          //do nothing
        }
      }
      for ($i = $picCount; $i < 5; $i++) {
        $img_src = "http://placehold.it/";
        $img_src_resize = $img_src."160x120";
?>
            <li class="span2">
              <div class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>">
                <div class="caption">
                  <a class="btn btn-info btn-mini" href="<?php echo $img_href; ?>">Add</a>
                  <a class="btn btn-success btn-mini disabled">Primary</a>
                </div>
              </div>
            </li>
<?php
      }
?>
          </ul>
          <hr />
          <div class="row">
            <div class="span2">
              <h5><i class="icon-ok-sign"></i> MYR <?php echo $resProd[0]['product_price']; ?></h5>
              <h5><i class="icon-ok-sign"></i> <?php echo $resProd[0]['product_quantity']; ?> piece(s) left</h5>
            </div>
            <div class="span4">
              <div class="well">
                <p><?php echo $resProd[0]['product_description']; ?></p>
                <p>
                  <span class="label label-warning">Category</span>
<?php
      foreach ($resCat2Prod as $key => $value) {
        //construct SELECT query
        $cols = array();
        $cols = array(1);
        $conds = array();
        $conds[] = array(0); //$param_col;
        $conds[] = array(0); //$param_op;
        $conds[] = array($resCat2Prod[$key]['category_id']); //$param_key;
        $query = $db->db_select("category", $cols, $conds);
        //execute SELECT query
        $result = $db->database->query($query);
        //result SELECT query
        $resCat = $result->fetchAll(SQLITE_ASSOC);
?>
                  <span class="label label-default"><?php echo $resCat[0]['category_name']; ?></span>
<?php
      }
?>
                  <a class="btn btn-info btn-mini" href="<?php echo $cat_href; ?>">Edit</a>
                </p>
              </div>
            </div>
          </div>
<?php
/*---PRODUCT FRONTAGE END---*/
    }
  } else {
    $error_msg = "Product error.";
    printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
  }
} else {
  $error_msg = "Page error.";
  printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']."?prod_id=".$_GET["prod_id"]);
}
?>
        </div>
      </div>
<?php
//include page footer
footer_template();
?>
